package cn.lau.core.utlis.mail;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.mail.*;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * 发送电子邮件工具
 * Created by LauChiPang on 2017/9/2.
 */
public class MailSender {

    private Logger logger = LoggerFactory.getLogger(MailSender.class);

   /*
    * smtp服务器地址:
    * smtp.yeah.net
    * smtp-mail.outlook.com
    *
    */
    private String host="smtp-mail.outlook.com";//smtp服务器地址:
    private String port="587";  //端口号

    private final String user = "app01liv@hotmail.com";//发送者邮箱的用户名
    private final String pwd = "01livappdevelopeR";    //发送者邮箱的密码'

    public MailSender() {
    }

    public MailSender(String host, String port) {
        this.host = host;
        this.port = port;
    }

    public void send(String mail,String title,String content){


        logger.info("开始发送邮件");

        /*
        *Properties是一个属性对象，用来创建Session对象
        */
        Properties props=new Properties();
        props.setProperty("mail.smtp.host", host);
        props.setProperty("mail.smtp.port", port);
        props.setProperty("mail.smtp.auth", "true"); //是否需要请求认证
        props.setProperty("mail.smtp.ssl.enable", "false");//"true"
        props.setProperty("mail.smtp.connectiontimeout", "5000");
        props.setProperty("mail.smtp.starttls.enable","true");

        /*
        *Session类定义了一个基本的邮件对话。
        */

        Session session= Session.getInstance(props, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(user,pwd);
            }
        });

        try {
        /*
        *Transport类用来发送邮件。
        *传入参数smtp，transport将自动按照smtp协议发送邮件。
        */
            Transport transport=session.getTransport("smtp");
            transport.connect(host,user,pwd);
        /*
        *Message对象用来储存实际发送的电子邮件信息
        */
            MimeMessage message=new MimeMessage(session);
            message.setSubject(title);
            //消息发送者接收者设置(发件地址，昵称)，收件人看到的昵称是这里设定的
            message.setFrom(new InternetAddress(user));
            message.addRecipients(Message.RecipientType.TO,new InternetAddress[]{
                    //Message.RecipientType中，CC抄送  BCC暗送
                    //消息接收者(收件地址，昵称)
                    new InternetAddress(mail),
            });
            message.saveChanges();

            //设置邮件内容及编码格式
            //后一个参数可以不指定编码，如"text/plain"，但是将不能显示中文字符
            message.setContent(content, "text/html;charset=UTF-8");
            //发送
            Transport.send(message);
            transport.close();

        }catch (AddressException e) {

            logger.info("邮件地址异常误:"+e.getMessage());
            e.printStackTrace();

        } catch (MessagingException e) {

            logger.info("邮件信息异常:"+e.getMessage());
            e.printStackTrace();
        }

        logger.info("邮件发送结束");
    }
}
